<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>哈希表</title>
	</head>
	<body>
		<script>
			/**
			 * 哈希函数 -- 字符串转数字再压缩数字成数组下标
			 * @param {*} str  字符串
			 * @param {*} size
			 * @returns
			 * 1. 将字符串转成较大的数
			 * 2. 将较大的数压缩到小范围
			 */
			function hasFun(str, size) {
				let hasCode = 0
				/// 霍纳算法
				for (let i = 0; i < str.length; i++) {
					hasCode = 37 * hasCode + str.charCodeAt(i)
				}
				return hasCode % size
			}

			alert(hasFun('abc', 7))
		</script>
	</body>
</html>
